#include <stdio.h>
#include <arpa/inet.h>
#include "can.h"

void can_dump(can_t *can)
{
   printf("TYPE:%c FMT:%c ID=%x [%x]: %02x %02x %02x %02x %02x %02x %02x %02x\n",
          can->msgtype,
          can->format,
          (unsigned int)ntohl(can->id),
          can->num,
          (unsigned char) can->data[0],
          (unsigned char) can->data[1],
          (unsigned char) can->data[2],
          (unsigned char) can->data[3],
          (unsigned char) can->data[4],
          (unsigned char) can->data[5],
          (unsigned char) can->data[6],
          (unsigned char) can->data[7]
   );
}

int is_valid(can_t *can)
{
   if ((can->msgtype!='M')&&(can->msgtype!='R')&&(can->msgtype!='S'))
   {
      return 0;
   }
   switch (can->format)
   {
      case 'S':  if ((unsigned long)(ntohl(can->id))>0x7FF) { return 0; }
                 break;
      case 'E':  if ((unsigned long)(ntohl(can->id))>0x1FFFFFFF) { return 0; }
                 break;
      default:   return 0;
                 break;
   }
   if ((unsigned char)can->num > 8) { return 0; }
   return 1;
}

